package cn.wom.core.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Service;

import cn.wom.core.bean.MissionOrder;
import cn.wom.core.bean.QueryPojo;
import cn.wom.core.bean.User;
import cn.wom.core.bean.WorkOrder;
import cn.wom.core.dao.OrderMapper;
import cn.wom.core.service.OrderService;

@Service
public class OrderServiceImpl implements OrderService {
	@Autowired
	private OrderMapper order;

	@Override
	public boolean saveMissionOrder(MissionOrder m_order) {
		int row = 0;
		row = order.insertMissionOrder(m_order);

		return row > 0 ? true : false;
	}

	@Override
	public boolean register(User user) {
		int row = 0;
		row = order.register(user);

		return row > 0 ? true : false;
	}

	// 应该要用事务
	@Override
	public boolean insertWorkOrderAndUpdateChild(WorkOrder w_order) {
		int row1 = order.insertWorkOrder(w_order);
		int row2 = order.updateWorkTotalCountAndChild(w_order);
		int row3 = updateIsfinish(w_order.getM_id());
		if (row1 > 0) {
			return true;
		} else {
			return false;
		}
	}

	@Override
	public boolean updateWorkOrderAndUpdateChild(WorkOrder w_order) {
		int row1 = order.updateWorkOrder(w_order);
		int bak_worktotalcount = w_order.getBak_worktotalcount();
		int good_count = w_order.getGood_count();
		int worktotalcount = 0;
		if (bak_worktotalcount != good_count) {
			worktotalcount = good_count - bak_worktotalcount;
			w_order.setWorktotalcount(worktotalcount);
			int row2 = order.updateWorktotalcount(w_order);
		}
		int row3 = updateIsfinish(w_order.getM_id());
		if (row1 > 0) {
			return true;
		} else {
			return false;
		}
	}

	public int updateIsfinish(String m_id) {
		int row = 0;
		String isfinish;
		MissionOrder missionOrder = order.selectMissionOrder(m_id);
		if (missionOrder != null) {
			if (missionOrder.getM_count() <= missionOrder.getWorktotalcount()) {
				isfinish = "yes";
				missionOrder.setIsfinish(isfinish);
				row = order.updateIsfinish(missionOrder);
				return row;
			} else {
				isfinish = "no";
				missionOrder.setIsfinish(isfinish);
				row = order.updateIsfinish(missionOrder);
				return row;
			}
		}else{
			return row;
		}
	}

	@Override
	public boolean deleteWorkOrder(String m_id, String w_id) {
		WorkOrder workOrder = order.selectWorkOrder(w_id);
		int good_count = workOrder.getGood_count();
		int row = order.deleteWorkOrder(w_id);
		MissionOrder missionOrder = order.selectMissionOrder(m_id);
		if (missionOrder != null) {
			int worktotalcount = missionOrder.getWorktotalcount();
			int new_worktotalcount = worktotalcount - good_count;
			missionOrder.setWorktotalcount(new_worktotalcount);
			missionOrder.setChild_count(missionOrder.getChild_count() - 1);
			int row1 = order.updateWorkTotalCount(missionOrder);
			int row2 = updateIsfinish(m_id);
		}

		if (row > 0) {
			return true;
		} else {
			return false;
		}

	}

	@Override
	public List<WorkOrder> selectWorkOrderList(QueryPojo q_pojo) {
		List<WorkOrder> list = order.selectWorkOrderList(q_pojo);
		return list;
	}

	@Override
	public WorkOrder selectWorkOrder(String w_id) {
		WorkOrder workOrder = order.selectWorkOrder(w_id);
		return workOrder;
	}

	@Override
	public MissionOrder selectMissionOrder(String m_id) {
		MissionOrder missionOrder = order.selectMissionOrder(m_id);
		return missionOrder;
	}

	@Override
	public boolean deleteMissionOrder(String m_id) {
		int row = 0;
		row = order.deleteMissionOrder(m_id);
		return row > 0 ? true : false;
	}

}
